package get.newsdaily.utils;

import get.newsdaily.model.RssItem;

import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class RssParser {
	public List<RssItem> getNewsList(String link) {
		try {
			
			  /*//URL url = new URL(link);
			  
			  DefaultHttpClient httpclient = new DefaultHttpClient();// //
			  HttpGet httpget = new HttpGet(link);//
			   HttpResponse response =
			  httpclient.execute(httpget);// 
			  HttpEntity entity =
			  response.getEntity();//
			  SAXParserFactory factory =
			  SAXParserFactory.newInstance(); //
			  factory.setValidating(false);//
			  
			  
			  SAXParser parser = factory.newSAXParser(); XMLReader reader =
			  parser.getXMLReader(); RssHandler rssHandler = new RssHandler();
			  reader.setContentHandler(rssHandler);
			  
			  InputSource inStream = new InputSource(); //
			  inStream.setCharacterStream(new StringReader(EntityUtils //
			  .toString(entity))); // 
			  reader.parse(inStream); 
			  return rssHandler.getItemList();*/
			 

/*			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(link);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(false);
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			RssHandler handler = new RssHandler();
			reader.setContentHandler(handler);
			InputSource inStream = new InputSource();
			inStream.setCharacterStream(new StringReader(EntityUtils
					.toString(entity)));
			reader.parse(inStream);
			return handler.getItemList();*/
			
			
			DefaultHttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(link);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(false);
			SAXParser parser = factory.newSAXParser();
			XMLReader reader = parser.getXMLReader();
			RssHandler rssHandle = new RssHandler();
			reader.setContentHandler(rssHandle);
			
			InputSource inStream = new InputSource();
			inStream.setCharacterStream(new StringReader(EntityUtils
			.toString(entity)));
			reader.parse(inStream);
			return rssHandle.getItemList();
			
			
		} catch (MalformedURLException e) {
			e.printStackTrace();

			return null;
			} catch (ParserConfigurationException e) {
			e.printStackTrace();

			return null;
			} catch (SAXException e) {
			e.printStackTrace();

			return null;
			} catch (IOException e) {
			e.printStackTrace();

			return null;
			}

			}
}